<?php

class ActivityController extends Controller
{

	/**
     * 过滤器
	 * @return 返回值是一个存放过滤器的数组
	 */
	public function filters()
	{
		return array(
            'accessAuth',//自定义授权方式
			//'accessControl', //默认授权方式
			'postOnly + delete',//删除操作只支持post请求
		);
	}
    /**
     * 自定义权限验证规则
     * @param type $filterChain
     * @throws CHttpException
     */
    public function filterAccessAuth($filterChain) {  
        if(Yii::app()->user->getIsGuest()) {  
            echo '<script>top.location.href="'.Yii::app()->user->returnUrl.'";</script>';
            exit;
        } else if(!Yii::app()->user->getIsGuest() && !in_array($this->getAction()->getId(), $this->authlessActions())){
            throw new CHttpException(403,'无权访问此页面');
        }
        $filterChain->run();  
    }  
    /**
     * 页面白名单
     * @return type
     */
    public function authlessActions() {  
        return array(
            'index','admin','view','create','update','delete','clander','select',
            'selectNow','dictionary','adminDictionary'
		); 
    }

	/**
	 * 查看页面
	 * @param integer $id
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 添加页面
	 * 如果添加成功，跳转到查看页面
	 */
	public function actionCreate()
	{
		$model=new Activity;

		//如果前台开启ajax则取消此注释
		//$this->performAjaxValidation($model);

		if(isset($_POST['Activity']))
		{
			$model->attributes=$_POST['Activity'];
                        $model->create_time=date('Y-m-d H:i:s',time());
                        $model->create_user=Yii::app()->user->username;
			if($model->save()){
                            Yii::app()->user->setFlash('success','操作成功！');
                            $this->redirect(array('Activity/admin'));
                        }
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 修改页面
	 * 如果修改成功，跳转到查看页面
	 * @param integer $id 
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		//如果前台开启ajax则取消此注释
		//$this->performAjaxValidation($model);

		if(isset($_POST['Activity']))
		{
			$model->attributes=$_POST['Activity'];
			if($model->save()){
                            Yii::app()->user->setFlash('success','操作成功！');
                            $this->redirect(array('Activity/admin'));
                        }
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除功能
	 * 一般只接受post方式的异步提交
	 * @param integer $id 
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isAjaxRequest && isset($_POST['id'])){
            $id=$_POST['id'];
            //获取id数组批量删除
            $criteria= new CDbCriteria;
            $criteria->addInCondition('id', $id);
            $res=Activity::model()->deleteAll($criteria);
            //输出json结果
            echo CJSON::encode(array('result'=>$res));
        }else{
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        } 
	}

	/**
	 * 列表页
	 */
	public function actionAdmin()
	{
		$model=new Activity('search');
		$model->unsetAttributes();  //清空并初始化
		if(isset($_GET['Activity']))
			$model->attributes=$_GET['Activity'];

		$this->render('admin',array(
			'model'=>$model,
			'data'=>$model->search(),
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Activity the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Activity::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Activity $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='activity-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        /**
         * 搜索
         */
        public function actionSelect(){
            $year = $_GET['year'];
//            $month = $_GET['month'];
            $month=sprintf("%02d", $_GET['month']);
            $week = $_GET['week']; 
            //
            $d=$year.'-'.$month.'-01';
            $m=$year.'-'.$month;
            //
            $days = date('t', strtotime($d));
            $weeks=$this->getWeeks($week,$days,$m); 
            $this->render('clander',array(
                'weeks'=>$weeks,
                'year'=>$year,
                'month'=>$month,
                'week'=>$week,
            ));            
        }
        /**
         * 搜索当前日期
         */
        public function actionSelectNow(){
            $date=date('Y-m-d',time());
            $year=date('Y',time());
            $month=date('m',time());
            $day = date('d',time());
            //获取日期列表
            $week = ceil($day/7);
            $weeks=array();
            $weeks[0]=$date;
            $this->render('clander',array(
                'weeks'=>$weeks,
                'year'=>$year,
                'month'=>$month,
                'week'=>$week,
            ));            
        }

        /**
         * 报名日历
         */
	public function actionClander()
	{       
//            $time = date('Y-m-d',time());
            $year=date('Y',time());
            $month=date('m',time());
            $day = date('d',time());
            //获取日期列表
            $week = ceil($day/7);
            $m=date('Y-m',time());
            //获取当前月的总天数
            $days = date("t"); 
            $weeks=$this->getWeeks($week,$days,$m); 
//            $model=  ActivityDetail::model()->findAll("start like '".$time."%'");
            $this->render('clander',array(
                'weeks'=>$weeks,
                'year'=>$year,
                'month'=>$month,
                'week'=>$week,
            ));
	}
        /**
         * 返回一周的日期列表
         * @param type $week
         * @param type $days
         */
        public function getWeeks($week,$days,$m){
            $weeks=array();
            switch ($week){
                case 1:
                    $weeks=$this->getList(1,7,$m);
                    break;
                case 2:
                    $weeks=$this->getList(8,14,$m);
                    break;
                case 3:
                    $weeks=$this->getList(15,21,$m);
                    break;         
                case 4:
                    if($days<28){
                       $weeks=$this->getList(22,$days,$m); 
                    }  else {
                       $weeks=$this->getList(22,28,$m); 
                    }  
                    break; 
                case 5:
                    if($days>28){
                       $weeks=$this->getList(29,$days,$m); 
                    }
                    break;
            }
            return $weeks;
        }
        public function getList($start,$end,$m){
            $weeks=array();
            for ($i=$start;$i<=$end;$i++){
                    $day=sprintf("%02d", $i);
                    $weeks[$i]=$m.'-'.$day;                
            }
            return $weeks;
        }
	/**
	 * 字典表
	 */
	public function actionDictionary()
	{
		$model=new Activity;
		$this->render('dictionary',array(
			'model'=>$model,
		));
	}
	/**
	 * 列表页
	 */
	public function actionAdminDictionary()
	{
		$model=new Activity('search');
		$model->unsetAttributes();  //清空并初始化
		if(isset($_GET['Activity']))
			$model->attributes=$_GET['Activity'];

		$this->render('adminDictionary',array(
			'model'=>$model,
			'data'=>$model->search(),
		));
	}


//    $days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);
//    echo "返回2011-4的天数 ".$days."<br/>";
//    $days = date('t', strtotime("2011-4-1"));
//    echo "返回2011-4的天数 ".$days."<br/>";
//    $days = date("t"); 
//    echo "当前月的天数 ".$days."<br/>";
      
        /**
         * 获取给定日的这个周的所有日期列表
         * @param type $week
         * @return type
         */
//        public function riqi($week){
//            $whichD=date('w',strtotime($week));
//            $weeks=array();
//            for($i=0;$i<7;$i++){
//                if($i<$whichD){
//                    $date=strtotime($week)-($whichD-$i)*24*3600;
//                }else{
//                    $date=strtotime($week)+($i-$whichD)*24*3600;
//                }
//                $weeks[$i]=date('Y-m-d',$date);
//
//            }
//            return $weeks;
//        }
}
